<?php
define("SYS_PATH",dirname(__FILE__)."/");
set_time_limit(300);
include_once 'giftcert/lib/Common.php';
include_once 'giftcert/lib/ImageText.php';
include_once 'giftcert/lib/Mail.php';
include_once 'giftcert/lib/Constant.php';

//Start session
session_start();

//get gateway config
$gateway = Common::gatewayConfig();

//quote value in post, get, session, cookie
Common::checkMagicQuotes();

//Get data from session
$ssData = $_SESSION['inputArray'];

//Get token from session
$token = $ssData['SID'];

// first load page, count transaction
if( isset($_SESSION[$token."amt"]) == FALSE )
{
	$req = 'cmd=_notify-synch';
	$req .= '&tx='.$_GET['tx'];
	$req .= '&at='.$gateway['pdt_token'];

	$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
	$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
	$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
	$fp = fsockopen ('ssl://'.$gateway["gateway_url"], 443, $errno, $errstr, 30);
	$success = false;
	if (!$fp) {
		echo "error";
	} else {
		fputs ($fp, $header . $req);
		while (!feof($fp)) {
			$res = fgets ($fp, 1024);
			if(strcmp( substr($res,0,8), "mc_gross") == 0)
			{	
				$success = true;
				$amt = substr($res,9);
			}
		}
		fclose ($fp);
	}
	if($success == false)
	{
		//Return to gift-certificates page
		header("location: gift-certificates.html");
		exit;
	}
	$_SESSION[$token."amt"] = $amt;
}
else
{
	$amt = $_SESSION[$token."amt"];
}

// first load page, count transaction
if( isset($_SESSION[$token]) == FALSE )
{
	$seqNo = Common::countTransaction();
	$_SESSION[$token] = $seqNo;
}
else//get seq number from session when user refresh page.
{
	$seqNo = $_SESSION[$token];
}

//get info from GET

$amount = $amt;
$clamcode = "GC".($ssData['location']=="LA"?"30":"20").date("y").sprintf("%04d",$seqNo);
$location = $ssData['location']=="LA"?"Los Angeles":"San Francisco";

// init array info for image creation.
$infoArr['template'] = $ssData['template'];
$infoArr['code'] = $clamcode;
$infoArr['amount'] = $amount;
$infoArr['from'] = $ssData['uName'];
$infoArr['to'] = $ssData['rName'];
$infoArr['message'] = $ssData['rMessage'];

//first load page, don't have session.
if( isset($_SESSION[$token."img"]) == FALSE )
{
	//Create image for gift certificate.
	$outImg = ImageText::createGiftCert($infoArr);
	
	//save to session.
	$_SESSION[$token."img"] = $outImg;
	
	//create log file
	$logArr['code'] = $infoArr['code'];
	$logArr['time'] = date("m-d-Y h:i:s");
	$logArr['uName'] = $ssData['uName'];
	$logArr['uEmail'] = $ssData['uEmail'];
	$logArr['rName'] = $ssData['rName'];
	$logArr['rEmail'] = $ssData['rEmail'];
	
	$logArr['location'] = $ssData['location']=="LA"?"Los Angeles":"San Francisco";
	$logArr['message'] = $infoArr['message'];
	$logArr['template'] = $infoArr['template'];
	$logArr['amount'] = $infoArr['amount'];
	
	$newfile = false;
	$logfile = Constant::LOG_PATH.'/log_'.date("mY").'.csv';
	if(file_exists($logfile) == false)
	{
		$newfile = true;
	}
	$fp = fopen($logfile, 'a');
	flock($fp, LOCK_EX);// do an exclusive lock
	if($newfile)
	{
		$header = array("GC Number","Date Time","Sender","Email","Recipient","Email","Redemption Location","Message","Template","Amount");
		fputcsv($fp,$header);
	}
	fputcsv($fp,$logArr);
	flock($fp, LOCK_UN); // release the lock
	fclose($fp);
	@chmod($logfile, 0777);

	//prepare info for send mail function.
	$emailArr["code"] = $clamcode;
	$emailArr["amount"] = $amount;
	$emailArr["uName"] = $ssData['uName'];
	$emailArr["rName"] = $ssData['rName'];
	$emailArr["uEmail"] = $ssData['uEmail'];
	$emailArr["rEmail"] = $ssData['rEmail'];
	$emailArr["location"] = $ssData['location'];
	$emailArr["attach"] = SYS_PATH.$outImg;
	
	//if not local host.
	if ( $_SERVER['HTTP_HOST'] != "localhost")
	{
		//send email
		register_shutdown_function('send_email',$emailArr);
	}
}
else//when user refresh page.
{
	//get image from session.
	$outImg = $_SESSION[$token."img"];
}

//send email function.
function send_email($emailArr)
{
	//type 1, send to customer
	$emailArr["type"] = 1;
   	Mail::sendEmail($emailArr);
   	
   	//type 2, send to admin
   	$emailArr["type"] = 2;
   	Mail::sendEmail($emailArr);

   	if( mb_strlen($emailArr["rEmail"]) > 0 )
   	{	
   		$emailArr["type"] = 0;//send to recipient
   		Mail::sendEmail($emailArr);
   	}
   	
   	//clean images a month ago.
   	ImageText::cleanImage();
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Digital Photography Services - Digital Imaging Services</title>
<meta name="verify-v1" content="N/yxUL4E2rQx8gpq7ivVQeX/HKfddPvCAsf3Pihde9c=" />
<META name = "description" content= "Professional digital photography services and digital film services by Digital Pickle.  We scan photos, slides and negatives, and digitizes audio, video, and film or sharing, preservation, and restoration. Reliable digital photography service nationwide." />
<META name= "keywords" content= "digital photography services, digital imaging services, digital film services, digital scanning services, digital imaging solutions, digitalpickle" />
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />


<link rel="shortcut icon" href=" dp.ico" />
<link href="dpstyles.css" rel="stylesheet" type="text/css" />


<style type="text/css" media="screen">
	@import url("./navbar.css");
	@import url("./styles.css");
	.imgTpl{
	border:1px solid black;
	}
	div.error{
		color: red;
		font-size:11px
	}
}
</style>

<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script>
<script type="text/javascript" src="js/lightbox.js"></script>

<script type="text/javascript" src="js/common.js"></script>

<link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />

<!--js and css needed for lightbox BEGIN -->
<link rel="stylesheet" href="css/overlayBox.css" media="screen,projection" type="text/css" />
<link rel="stylesheet" href="css/lightbox.css" media="screen,projection" type="text/css" />

<script src="Scripts/prototype.js" type="text/javascript"></script>
<script src="Scripts/lightbox.js" type="text/javascript"></script>
<!--js and css needed for lightbox END -->


<!--[if IE 5]>
<style type="text/css"> 
/* place css box model fixes for IE 5* in this conditional comment */
.centerCol #sidebar1 { width: 220px; }
</style>
<![endif]--><!--[if IE]>
<style type="text/css"> 
/* place css fixes for all versions of IE in this conditional comment */
.centerCol #sidebar1 { padding-top: 30px; }
.centerCol #mainContent { zoom: 1; }
/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */
</style>
<![endif]-->

<script type="text/javascript">
<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>

<!--[if lte IE 6]>
<script type="text/javascript" src="Scripts/supersleight-min.js"></script>
<![endif]-->
</head>

<body class="centerCol" onLoad="MM_preloadImages('includes/images/myproject_on.gif','includes/images/services_on.gif','includes/images/howitworks_on.gif','includes/images/aboutus_on.gif','includes/images/testimonials_on.gif','includes/images/prices_on.gif','includes/images/photoscanning_on.gif','includes/images/videotransfer_on.gif','includes/images/filmtransfer_on.gif','includes/images/audiotransfer_on.gif','includes/images/dvdslideshows_on.gif','includes/images/restoration_on.gif','includes/images/photogifts_on.gif','includes/images/proservices_on.gif')">

<div id="container">
<?php include("includes/header.html"); ?>
<div id="twoColContainer">
  <div id="sidebar1">
  <?php include("includes/aff_and_assoc.html"); ?>
  </div>
<!-- end #sidebar1 -->

  <div id="mainCol">
  
  <img src="images/pagetitle_giftcert.png" alt="gift certificates" width="735" height="60" id="serviceTitle"/>

    
  <div class="miniContainer">
    <h4>Gift Certificates for Digital Services</h4>

    <p>
<img src="images/logo_paypal.jpg" alt="picture of photobook" width="119" height="50" border="0" class="imgWhiteBorder"/>
Payment has been made through Paypal using your credit card (Visa, MasterCard, Discover, American Express). </p>
  </div>
  <div class="miniContainerEmpty">
    
  <?php
if( mb_strlen($outImg) > 0 )
{
	echo "<a href=\"".$outImg."\" rel=\"lightbox\">"; 
	echo "<img width=\"400\" src=\"".$outImg."\"></a>"; 
}
?>
</center>

<br>
<form action="http://www.digitalpickle.com/test/index.shtml" method="post">

<table width="480" border="0" cellpadding="5" cellspacing="5" align="center" id="giftCertTable">
<tr height="40">
<td>
<span class="how-it-works-bold">Send Gift Certificate Confirmation</span>
</td>
</tr>

<tr height="30"><td>You have sent a Gift Certificate to:
</td></tr>

<tr height="30"><td>- Recipient name: <?php 
echo htmlspecialchars($ssData['rName']);
?></td></tr>

<tr height="30"><td width="200">- Claim code: <?php echo $clamcode; ?>
</td></tr>

<tr height="30"><td>- Redeem at <?php echo $location; ?></td></tr>

<tr height="30"><td>- Amount: $<?php echo $amount; ?></td></tr>

<tr height="30"><td valign="bottom">
<input type="image" src="images/button_next.png" onMouseOver="this.src='images/button_next.png'" onMouseOut="this.src='images/button_next.png'" width="117" height="37" border="0" name="submit" alt="Return Homepage">
</td></tr>

</table>
</form>



</div>
    
	<!-- end #mainCol --></div>
	<!-- This clearing element should immediately follow the #mainCol div in order to force the #container div to contain all child floats --><br class="clearfloat" />


<!-- end #twoColContainer --></div>

<?php include ('includes/footer.html');?>

<!-- end #container -->


</body>
</html>